
// pages/index/select_street/select_street.js
const httpUtils = require('../../../../utils/https-utils.js')
const toolUtils = require('../../../../utils/tool-utils.js')
var that
var provinceList = []
var cityList = []
var areaList = []
var streetList = []
var provinceInfo = {}
var cityInfo = {}
var areaInfo = {}
var streetIn = {}
var streetInfo = {}
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeID: 1,
    checkedIndex2: -1,
    isButSubmit: true,
    list: [],
    searchList: [],
    addr: '请在下列选择街道！',
    province_name: '请选择',
    city_name: '请选择',
    area_name: '请选择',
    street_name: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    streetInfo = app.getStreetInfo()
    provinceList = []
    cityList = []
    areaList = []
    streetList = []
    if (streetInfo && streetInfo.street_id && streetInfo.area_id) {
      that.setData({
        province_name: streetInfo.province_name,
        city_name: streetInfo.city_name ||'',
        area_name: streetInfo.area_name ||'',
        street_name: streetInfo.street_name ||'',
        street_id: streetInfo.street_id||'',
        merger_name: streetInfo.merger_name || streetInfo.street_name || ''
      })
      getArea(streetInfo.area_id, 4)
    } else {
      getArea(0, 1)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.refresh) {
      this.setData({
        refresh: false
      })
      //搜索街道
      getStreetSearch(this.data.search_name)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //清除搜索框
  bindCancel: function (e) {
    this.setData({
      inputTxt: ''
    })
  },
  //确认搜索
  bindSearch: function (e) {
    toolUtils.pageTo('/page/pack-index/pages/search-street/search-street')
  },
  //点击省市区选择
  onAddr: function (e) {
    console.log(streetInfo)
    switch (e.currentTarget.id) {
      case 'province':
        if (provinceList.length > 0) {
          that.setData({
            typeID: 1,
            list: provinceList,
            checkedIndex2: -1
          })
        } else {
          getArea(0, 1)
        }
        break
      case 'city':
        if (!!cityList && cityList.length > 0) {
          that.setData({
            typeID: 2,
            list: cityList,
            checkedIndex2: -1
          })
        } else {
          if (streetInfo == null || !streetInfo.province_id) {
            toolUtils.showFailToast("请选择省")
            return
          }
          getArea(streetInfo.province_id, 2)
        }
        break
      case 'area':
        if (areaList.length > 0) {
          that.setData({
            typeID: 3,
            list: areaList,
            checkedIndex2: -1
          })
        } else {
          if (streetInfo == null || !streetInfo.city_id) {
            toolUtils.showFailToast("请选择市")
            return
          }
          getArea(streetInfo.city_id, 3)
        }
        break
    }
  },
  //省市区街道列表的点击事件
  onItem: function (e) {
    var index = e.currentTarget.dataset.index
    var item = e.currentTarget.dataset.item
    console.log(e)
    if (that.data.typeID == 1) {
      if (item.is_open == 2) {
        toolUtils.showFailToast("当前省未开通")
        return
      }
      if (!provinceInfo.id || provinceInfo.id != item.id) {
        getArea(item.id, 2)
      } else {
        that.setData({
          list: cityList
        })
      }
      that.setData({
        typeID: 2,
        isButSubmit: true,
        province_name: item.region_name_c,
        city_name: '请选择',
        area_name: '请选择'
      })
      provinceInfo = item
    } else if (that.data.typeID == 2) {
      if (item.is_open == 2) {
        toolUtils.showFailToast("当前市未开通")
        return
      }
      if (!cityInfo.id || cityInfo.id != item.id) {
        getArea(item.id, 3)
      } else {
        that.setData({
          list: areaList
        })
      }
      cityInfo = item
      that.setData({
        typeID: 3,
        isButSubmit: true,
        city_name: item.region_name_c,
        area_name: '请选择'
      })
    } else if (that.data.typeID == 3) {
      if (item.is_open == 2) {
        toolUtils.showFailToast("当前区县未开通")
        return
      }
      if (!areaInfo.id || areaInfo.id != item.id) {
        getArea(item.id, 4)
      } else {
        that.setData({
          list: streetList
        })
      }
      areaInfo = item
      that.setData({
        typeID: 4,
        isButSubmit: true,
        area_name: item.region_name_c
      })
    } else {
      if (item.is_open == 2) {
        toolUtils.showFailToast("当前街道未开通")
        return
      }
      if (streetInfo == null || (item.id != streetInfo.street_id && item.is_open == 1)) {
        streetList.splice(index, 1)
        streetList.unshift(item)
      }
      that.setData({
        list: streetList,
        checkedIndex2: -1,
        merger_name: item.merger_name
      })
      if (that.data.isButSubmit) {
        that.setData({
          isButSubmit: false
        })
      }
      streetIn = item
    }
    wx.pageScrollTo({
      scrollTop: 0
    })

  },
  onItem2: function (e) {
    var index = e.currentTarget.dataset.index
    var item = e.currentTarget.dataset.item
    if (item.is_open == 2) {
      toolUtils.showFailToast("当前街道未开通")
      return
    }
    provinceInfo = {}
    cityInfo = {}
    areaInfo = {}
    streetIn = {}
    streetInfo = item
    getArea(streetInfo.area_id, 4)
    if (that.data.isButSubmit) {
      that.setData({
        isButSubmit: false,
        checkedIndex2: index,
        merger_name: streetInfo.province_name + " " + streetInfo.city_name + " " + streetInfo.area_name + " " + streetInfo.street_name,
        province_name: streetInfo.province_name,
        city_name: streetInfo.city_name,
        area_name: streetInfo.area_name,
      })
    } else {
      that.setData({
        merger_name: streetInfo.province_name + " " + streetInfo.city_name + " " + streetInfo.area_name + " " + streetInfo.street_name,
        checkedIndex2: index,
        province_name: streetInfo.province_name,
        city_name: streetInfo.city_name,
        area_name: streetInfo.area_name,
      })
    }
  },
  //确认选择
  bindsubmit: function (e) {
    if (streetInfo == null) {
      streetInfo = {}
    }
    if (provinceInfo.id) {
      streetInfo.province_id = provinceInfo.id,
        streetInfo.province_name = provinceInfo.region_name_c
    }
    if (cityInfo.id) {
      streetInfo.city_id = cityInfo.id,
        streetInfo.city_name = cityInfo.region_name_c
    }
    if (areaInfo.id) {
      streetInfo.area_id = areaInfo.id,
        streetInfo.area_name = areaInfo.region_name_c
    }
    if (streetIn.id) {
      streetInfo.street_id = streetIn.id
      streetInfo.group_id = streetIn.id
      streetInfo.group_name = streetIn.region_name_c
      streetInfo.street_name = streetIn.region_name_c
      streetInfo.merger_name = streetIn.merger_name
      getCurrentPages()[0].setData({
        group_name: streetIn.region_name_c,
        refresh: true
      })
    } else {
      getCurrentPages()[0].setData({
        group_name: streetInfo.street_name,
        refresh: true
      })
    }
    app.data.streetInfo = streetInfo
    app.data.service_refresh = true
    //保存街道信息
    wx.setStorage({
      key: 'streetInfo',
      data: streetInfo,
      complete: function (e) {
        wx.navigateBack({
          delta: 1
        })
      }
    })
  }
})
/*
*upper_region:	父级区域ID,例如: 0 获取所有省份
*typeID: 1:省，2:市，3:区，4:街道
*/
function getArea(upper_region, typeID) {
  httpUtils.getArea(upper_region, function (res) {
    switch (typeID) {
      case 1:
        provinceList = res
        break
      case 2:
        cityList = res
        break
      case 3:
        areaList = res
        break
      case 4:
        if (streetInfo != null && streetInfo.street_id && streetInfo.area_id == upper_region) {
          for (var i = 0; i < res.length; i++) {
            if (res[i].id == streetInfo.street_id && res[i].is_open == 1) {
              var item = res[i]
              res.splice(i, 1)
              res.unshift(item)
              console.log(res)
              break
            }
          }
        }
        streetList = res
        break
    }
    that.setData({
      list: res,
      scrollId:"itemId0",
      typeID: typeID
    })
  }, function (e) {

  })
}
//街道搜索
function getStreetSearch(keyWord) {
  httpUtils.streetSearch(keyWord, function (res) {
    if (res.length > 0) {
      var checkedIndex2 = 0;
      if (res[0].is_open == 2) {
        checkedIndex2 = -1
        that.setData({
          searchList: res
        })
      } else {
        streetInfo = res[0]
        getArea(res[0].area_id, 4)
        that.setData({
          searchList: res,
          checkedIndex2: checkedIndex2,
          province_name: res[0].province_name,
          city_name: res[0].city_name,
          area_name: res[0].area_name,
        })
      }

    } else {
      that.setData({
        searchList: []
      })
    }

  }, function (e) {
    that.setData({
      searchList: []
    })
    toolUtils.showFailToast(e.data.msg)
  })
}